package hot;

import java.util.Arrays;

public class Thirtthree {
    public static int search(int[] nums, int target) {
        int left=0;
        int right=nums.length-1;
        while(left<right){
            int mid=left+(right-left)/2;
            if(nums[mid]>nums[nums.length-1]){
                left=mid+1;
            }else{
                right=mid;
            }
        }
        int m=left;
        if(m==0){
            if(target==nums[0]){
                return 0;
            }
            if(target!=nums[0]&& nums.length==1){
                return -1;
            }
            if(nums.length>1){
                for(int i=0;i<nums.length;i++){
                    if(nums[i]==target){
                        return i;
                    }
                }
                return -1;
            }
        }
        //    接下来判断target是在左边还是右边
        if(target>=nums[0]&&target<=nums[m-1]){
            left=0;
            right=m-1;
            while(left<=right){
                int mid=left+(right-left)/2;
                if(nums[mid]>target){
                    right=mid-1;
                }else if(nums[mid]<target){
                    left=mid+1;
                }else{
                    return mid;
                }
            }
        }
        if(target>=nums[m]&&target<=nums[nums.length-1]){
            left=m;
            right=nums.length-1;
            while(left<=right){
                int mid=left+(right-left)/2;
                if(nums[mid]>target){
                    right=mid-1;
                }else if(nums[mid]<target){
                    left=mid+1;
                }else{
                    return mid;
                }
            }
        }
        return -1;




    }

    public static void main(String[] args) {
//        [4,5,6,7,0,1,2]
        int[] num={1,3};
        int search = search(num, 2);
        System.out.println(search);


    }
}
